package com.estate.business.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.estate.business.AnnouncementService;
import com.estate.dao.AnnouncementDao;
import com.estate.utils.AnnouncementUtil;
import com.estate.vo.Vo;
import com.estate.vo.impl.AnnouncementVo;
import com.estate.vo.impl.UserInfoVo;

public class AnnouncementServiceImpl implements AnnouncementService {

	private AnnouncementDao announcementDao;

	public AnnouncementDao getAnnouncementDao() {
		return announcementDao;
	}

	public void setAnnouncementDao(AnnouncementDao announcementDao) {
		this.announcementDao = announcementDao;
	}

	public boolean addAnnouncement(AnnouncementVo announcement) {
		boolean result = false;
		result = this.getAnnouncementDao().addVo(announcement);
		return result;
	}

	public List<Vo> getAnnouncementList(UserInfoVo userInfo) {
		List<Vo> announcementList = new ArrayList<Vo>();
		List<Vo> postedAnnouncementList = this.getAnnouncementDao().findAllPostedAnnouncement();
		List<Vo> myUnpostedAnnouncementList = this.getAnnouncementDao().findAllMyUnPostedAnnouncement(userInfo);
		announcementList.addAll(postedAnnouncementList);
		announcementList.addAll(myUnpostedAnnouncementList);
		return announcementList;
	}

	public Vo getAnnouncementVo(int id) {
		AnnouncementVo vo = (AnnouncementVo) this.getAnnouncementDao().findByPK(id);
		
		return vo;
	}
	
	public boolean updateReadNum(AnnouncementVo vo){
		if(vo.getState().equals("已发布")){
			int redNum = vo.getReadNum()+1;
			vo.setReadNum(redNum);
		}
		this.getAnnouncementDao().updateVo(vo);
		return true;
	}

	public boolean updateAnnouncement(AnnouncementVo announcement) {
		AnnouncementVo old = (AnnouncementVo) this.getAnnouncementDao().findByPK(announcement.getId());
		announcement.setPostUser(old.getPostUser());
		boolean result = false;
		if(old.getState().equals("未发布")){
			if("已发布".equals(announcement.getState())){
				announcement.setPostDate(new Date());
				announcement.setReadNum(0);
			}
		}else{
			announcement.setPostDate(old.getPostDate());
			announcement.setReadNum(old.getReadNum());
		}
		result = this.getAnnouncementDao().updateVo(announcement);
		
		return result;
	}

	public boolean deleteAnnouncement(AnnouncementVo announcement) {
		boolean result = false;
		result = this.getAnnouncementDao().deleteVo(announcement);
		return result;
	}

	public boolean deleteMultiAnnouncement(String delStr) {
		if(delStr!= null && !"".equals(delStr) && !",".equals(delStr)){
			String[] delArray = delStr.split(",");
			AnnouncementVo announcement = null;
			for(int i = 0;i<delArray.length;i++){
				announcement = new AnnouncementVo();
				announcement.setId(Integer.parseInt(delArray[i]));
				this.deleteAnnouncement(announcement);
			}
		}
		return true;
	}

	public boolean updatePost(int id) {
		AnnouncementVo old = (AnnouncementVo) this.getAnnouncementDao().findByPK(id);
		old.setState(AnnouncementUtil.getAnnouncementString(2));
		old.setPostDate(new Date());
		boolean result = this.getAnnouncementDao().updateVo(old);
		return result;
	}
}
